package control 
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import com.greensock.easing.Cubic;
	import com.greensock.TweenLite;
	import events.MyEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.DisplayObject
	/**
	 * ...
	 * @author FireKnight
	 */
	public class ScrollDrag extends EventDispatcher 
	{
		public var container:Sprite;
		public var _mask_mc:Sprite;
		public var _hitArea:Sprite;
		public var distance:Number = 0;
		protected var startMouseY:Number = 0;
		protected var endMouseY:Number = 0;
		public var target:Sprite;
		public var fx:int = 0;
		public var dif:Number = 0;//上下偏差
		public function ScrollDrag(target:IEventDispatcher=null) 
		{
			super(target);
			
		}
		
		public function addHitAreaEvent():void
		{
			_hitArea.addEventListener(MouseEvent.MOUSE_DOWN, handleMaskMouseDown);
		}
		
		public function removeHitAreaEvent():void
		{
			if (_hitArea)
			{
				if (_hitArea.hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					_hitArea.removeEventListener(MouseEvent.MOUSE_DOWN, handleMaskMouseDown);
				}
			}
		}
		
		protected function handleMouseUp(e:MouseEvent):void
		{
			
			target.parent.stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
			target.parent.stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
		}
		
		protected function handleMaskMouseDown(e:MouseEvent):void
		{
			this.dispatchEvent(new MyEvent("moved",null,true));
			distance = 0;
			if (target.parent.mouseX > (_hitArea.x + target.x) && target.parent.mouseX < (_hitArea.x + _hitArea.width + target.x) && target.parent.mouseY > _hitArea.y + target.y && target.parent.mouseY < (_hitArea.y + _hitArea.height + target.y))
			{
				startMouseY = target.parent.mouseY;
				target.parent.stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp);
				target.parent.stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
			}
		}
		
		
		protected function handleMouseMove(e:MouseEvent):void
		{
			endMouseY = target.parent.mouseY;
			distance = startMouseY - endMouseY;
			moveFunction();
		}
		
		protected function moveFunction():void
		{
			var _target:Number = container.y - distance;
			if (_target > _mask_mc.y+dif)
				_target = _mask_mc.y+dif;
			if (_target < _mask_mc.y - container.height + _mask_mc.height-dif-100)
				_target = _mask_mc.y - container.height + _mask_mc.height-dif-100;
			TweenLite.to(container, 0.5, { y: _target, ease: Cubic.easeOut,onUpdate:dispath,onUpdateParams:[container.y],onComplete:dispath, onCompleteParams:[container.y]} );
		
		}
		
		
		protected function dispath(s:Number):void
		{
			this.dispatchEvent(new MyEvent("scrolled", s));
		}
		
		public function init():void
		{
			//trace("container="+container);
			//trace("_mask_mc="+_mask_mc);
			if (container.height > _mask_mc.height)
			{
				addHitAreaEvent();
			}
		}
		
		public function gc():void
		{
			removeHitAreaEvent();
		}
		
	}

}